package com.li.controller;
import com.li.pojo.Save;
import com.li.view.BankSystemMenu;

import java.io.*;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @Author:li
 * @Date:2024/8/28 14:33
 * @Description:使用集合完成数据存取功能
 **/

public class bankSystem {
    HashMap<String, Save> bankdb;

    public bankSystem()throws Exception{//构造函数,作用：初始化数据成员
        Scanner sc = new Scanner(System.in);
        bankdb = new HashMap<>();
        initBankdb();
    }
    public void initBankdb()throws Exception {//读文件，完成bankdb集合的赋值。
        File file = new File("C:\\Users\\lenovo\\IdeaProjects\\wjf2\\上课实例\\li de\\src\\com\\li\\db\\AccountInfo.txt");
        FileReader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        String str = "";
        while((str=br.readLine())!=null){
            String[]save_arr = str.split(",");
            Save save = new Save();
            save.setAccountid(save_arr[0]);
            save.setName(save_arr[1]);
            save.setPwd(save_arr[2]);
            save.setGender(save_arr[3]);
            save.setMoney(Double.parseDouble(save_arr[4]));
            bankdb.put(save_arr[0],save);
            Thread.sleep(1000);
            System.out.println(save);
        }
        System.out.println("账户初始化信息完毕！");
    }

    public void runBankSystem() throws Exception {//ctrl + alt + l 格式化代码
        String flag = "N";
        do {
            BankSystemMenu.showMenu();
            Scanner sc = new Scanner(System.in);
            String userinput = sc.next();
            switch (userinput) {
                case "1" -> {
                    createAccount();//ctrl+鼠标左键
                }
                case "2" -> {
                    deleteAccount();
                }
                case "3" -> {
                    saveMoney();
                }
                case "4" -> {
                    getMoney();
                }
                case "5" -> {
                    changeMoney();
                }
                case "6" -> {
                    showBalance();
                }
                case "7" -> {
                    exit();
                }
                default -> {
                    System.out.println("您输入的有误。");
                }
            }
            System.out.println("是否继续：（Y|N)");
            flag = sc.next();
            BankSystemMenu.clearScreen();
        } while (flag.equalsIgnoreCase("Y"));
        System.out.println("seeya~");
    }



    public void createAccount() {
        Scanner sc = new Scanner(System.in);
        System.out.println("创建账户");
        System.out.println("请输入用户名：");
        String name = sc.next();
        System.out.println("请输入密码：");
        String pwd = sc.next();
        System.out.println("请输入性别：");
        String gender = sc.next();
        Save save = new Save();
        String accountid = "6653" + (int) (1000 + (Math.random() * 9000));
        System.out.println("开户成功，已为您生成了专属卡号：" + accountid);
        save.setAccountid(accountid);
        save.setName(name);
        save.setPwd(pwd);
        save.setGender(gender);
        bankdb.put(accountid, save);
        System.out.println("您的开户信息为：" + bankdb);
    }//开户

    public void saveMoney() throws InterruptedException{
        int count=3;
        boolean iscontinue=false;
        Scanner sc = new Scanner(System.in);
        System.out.println("正在执行存钱...");
        System.out.println("-------------------------");
        System.out.println("请输入您的银行卡号：");
        String account = sc.next();
        if(bankdb.containsKey(account)){
            Save save = bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd= sc.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("请输入存款金额：");
                    double money= sc.nextDouble();
                    save.setMoney(money+ save.getMoney());
                    bankdb.put(account,save);
                    System.out.println("存款成功");
                    System.err.printf("您的卡号：%s,您的姓名：%s,您的余额：%f\n",save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue=false;
                }else{
                    count--;
                    if(count==0){
                        System.out.println("尝试次数耗尽，即将退出查询功能...");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您输入的密码有误，还剩"+count+"请重试!");
                    iscontinue=true;
                }}while(iscontinue);
        }else{
            System.out.println("您输入的银行卡号有误：");
        }
    }//存钱

    public void getMoney() throws InterruptedException{
        int count=3;
        boolean iscontinue=false;
        Scanner sc = new Scanner(System.in);
        System.out.println("正在执行取钱...");
        System.out.println("-------------------------");
        System.out.println("请输入您的银行卡号：");
        String account = sc.next();
        if(bankdb.containsKey(account)){
            Save save = bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd= sc.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("请输入取款金额：");
                    double money= sc.nextDouble();
                    if(money> save.getMoney()){
                        System.out.println("您的余额不足！");
                        return;
                    }else {
                        save.setMoney(save.getMoney()-money);
                        bankdb.put(account,save);
                    }
                    System.out.println("取款成功");
                    System.err.printf("您的卡号：%s,您的姓名：%s,您的余额：%f\n",save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue=false;
                }else{
                    count--;
                    if(count==0){
                        System.out.println("尝试次数耗尽，即将退出查询功能...");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您输入的密码有误，还剩"+count+"请重试!");
                    iscontinue=true;
                }}while(iscontinue);
        }else{
            System.out.println("您输入的银行卡号有误：");
        }
    }//取钱

    public void showBalance() throws InterruptedException{
        int count=3;
        boolean iscontinue=false;
        Scanner sc = new Scanner(System.in);
        System.out.println("正在执行查询余额...");
        System.out.println("-------------------------");
        System.out.println("请输入您的银行卡号：");
        String account = sc.next();
        if(bankdb.containsKey(account)){
            Save save = bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd= sc.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("正在查询账户信息，请稍后...");
                    Thread.sleep(1000);
                    System.err.printf("您的卡号：%s,您的姓名：%s,您的余额：%f\n",save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue=false;
                }else{
                    count--;
                    if(count==0){
                        System.out.println("尝试次数耗尽，即将退出查询功能...");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您输入的密码有误，还剩"+count+"请重试!");
                    iscontinue=true;
                }}while(iscontinue);
        }else{
            System.out.println("您输入的银行卡号有误：");
        }
    }//查询余额

    public void changeMoney() {

    }

    public void deleteAccount() throws InterruptedException{
        int count=3;
        boolean iscontinue=false;
        Scanner sc = new Scanner(System.in);
        System.out.println("正在执行销户...");
        System.out.println("-------------------");
        System.out.println("请输入您的银行卡号：");
        String account = sc.next();
        if(bankdb.containsKey(account)){
            Save save = bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd= sc.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("正在执行销户，请稍后...");
                    Thread.sleep(1000);
                    bankdb.remove(account);
                    System.out.println("销户完成!");
                    iscontinue=false;
                }else{
                    count--;
                    if(count==0){
                        System.out.println("尝试次数耗尽，即将退出系统");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您输入的密码有误，还剩"+count+"请重试!");
                    iscontinue=true;
                }}while(iscontinue);
        }else{
            System.out.println("您输入的银行卡号有误：");
        }
    }//销户

    public void exit() {
        Scanner sc = new Scanner(System.in);
        System.out.println("是否真的退出系统？(Y|N)");
        String Userinput= sc.next();
        if(Userinput.equalsIgnoreCase("Y")) {
            System.exit(0);
        }
    }
}
//6